/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.tool;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import top.wardyan.base.action.BaseBillShell;
import top.wardyan.base.util.swt.Msg;
import top.wardyan.tool.po.ConfItem;
import org.eclipse.swt.widgets.Control;

/**
 * @author ward
 *
 * 2019年2月21日
 */
public class AppConfigPropertiesConfItem extends BaseBillShell<AppConfigPropertiesConfItem>{
	protected Label lblName;
	protected Label lblCode;
	protected Text txtName;
	protected Text txtCode;
	protected Button btnNew;
	protected Button btnDel;
	protected Text txtMaxLength;
	protected Label lblMaxLength;
	protected Button chkRequired;
	protected Button chkEncode;
	
	
	protected ConfItem item;
	protected List<ConfItem> items;
	
	/** @wbp.parser.constructor */
	@SuppressWarnings("exports")
	public AppConfigPropertiesConfItem(ConfItem item) {
		super(1);
		this.item = item;
		this.init();
		this.txtName.setFocus();
	}
	
	@SuppressWarnings("exports")
	public AppConfigPropertiesConfItem(List<ConfItem> items, ConfItem item) {
		this(item);
		this.items = items;
		if(item != null) {
			txtName.setText(item.getName());
			txtCode.setText(item.getCode());
			txtMaxLength.setText(item.getMaxLength().toString());
			chkRequired.setSelection(item.getRequired());
			chkEncode.setSelection(item.getEncoded());
			btnDel.setEnabled(true);
		}
	}
	
	
	
	
	
	
	
	protected void init() {
		this.lblName = new Label(this, SWT.NONE);
		this.lblName.setAlignment(SWT.RIGHT);
		this.lblName.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.lblName.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.lblName.setBounds(170, 96, 61, 21);
		this.lblName.setText("名称");
		
		this.lblCode = new Label(this, SWT.NONE);
		this.lblCode.setText("代码");
		this.lblCode.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.lblCode.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.lblCode.setAlignment(SWT.RIGHT);
		this.lblCode.setBounds(187, 134, 44, 21);
		
		this.txtName = new Text(this, SWT.BORDER);
		this.txtName.setTextLimit(50);
		this.txtName.setBounds(237, 94, 150, 23);
		
		this.txtCode = new Text(this, SWT.BORDER);
		this.txtCode.setTextLimit(50);
		this.txtCode.setBounds(237, 132, 150, 23);
		
		this.btnNew = new Button(this, SWT.NONE);
		this.btnNew.addSelectionListener(getSelectionAdapter());
		this.btnNew.setBounds(356, 242, 61, 27);
		this.btnNew.setText("新增");
		
		this.btnDel = new Button(this, SWT.NONE);
		this.btnDel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				self.items.remove(item);
				self.dispose();
			}
		});
		this.btnDel.setEnabled(false);
		this.btnDel.setText("删除");
		this.btnDel.setBounds(289, 242, 61, 27);
		
		this.txtMaxLength = new Text(this, SWT.BORDER);
		this.txtMaxLength.setTextLimit(4);
		this.txtMaxLength.setBounds(237, 171, 150, 23);
		
		this.lblMaxLength = new Label(this, SWT.NONE);
		this.lblMaxLength.setText("最长");
		this.lblMaxLength.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.lblMaxLength.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.lblMaxLength.setAlignment(SWT.RIGHT);
		this.lblMaxLength.setBounds(187, 173, 44, 21);
		
		this.chkRequired = new Button(this, SWT.CHECK);
		this.chkRequired.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.chkRequired.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.chkRequired.setSelection(true);
		this.chkRequired.setBounds(237, 209, 68, 27);
		this.chkRequired.setText("必需");
		
		this.chkEncode = new Button(this, SWT.CHECK);
		this.chkEncode.setText("加密");
		this.chkEncode.setFont(SWTResourceManager.getFont("新宋体", 16, SWT.NORMAL));
		this.chkEncode.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.chkEncode.setBounds(311, 212, 68, 21);
		setTabList(new Control[]{this.txtName, this.txtCode, this.txtMaxLength, this.chkRequired, this.chkEncode, this.btnNew, this.btnDel});
	}
	
	protected SelectionAdapter getSelectionAdapter() {
		return new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				if(self.item != null) {
					item.setName(txtName.getText());
					item.setCode(txtCode.getText());
					item.setMaxLength(Integer.valueOf(txtMaxLength.getText()));
					item.setRequired(Boolean.valueOf(chkRequired.getSelection()));
					item.setEncoded(Boolean.valueOf(chkEncode.getSelection()));
					self.dispose();
				}else {
					Msg.showMessage("错误", "参数为空");
				}
			}
		};
	}

	/* (non-Javadoc)
	 * @see top.wardyan.base.action.BaseBillShell#getTitle()
	 */
	@Override
	protected String getTitle() {
		return "配置明细";
	}
}
